﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ASMS.Excel2DbTool.Utils
{
    /// <summary>
    /// 网络中华机构详细信息
    /// </summary>
    public class wlzh_jigouxiangxixinxi_v1
    {
        public static void Run(string filePath, string destFileName)
        {
            DataTable dataTable = Sunboxsoft.Excel.ReadTool.ReadExcel(filePath, true);

            StringBuilder contentBuilder = new StringBuilder();

            for (int index = 0; index < dataTable.Rows.Count; index++)
            {
                string nameString = Convert.ToString(dataTable.Rows[index][0]).Trim();
                string geoString = Convert.ToString(dataTable.Rows[index][1]).Trim();
                string typeString = Convert.ToString(dataTable.Rows[index][4]).Trim();

                #region 检查
                if (string.IsNullOrEmpty(nameString) || string.Equals(nameString, "无"))
                {
                    continue;
                }
                if (string.IsNullOrEmpty(typeString) || string.Equals(typeString, "无"))
                {
                    continue;
                }

                if (string.IsNullOrEmpty(geoString))
                {
                    continue;
                }

                string[] geoArr = geoString.Split(new char[] { ',' });
                if (geoArr.Length != 2)
                {
                    continue;
                }
                double geoLng = 0;
                double geoLat = 0;

                if (!double.TryParse(geoArr[0], out geoLng))
                {
                    continue;
                }
                if (!double.TryParse(geoArr[1], out geoLat))
                {
                    continue;
                }
                #endregion

                if (index == 0)
                {
                    contentBuilder.AppendFormat("{{\"name\":\"{0}\",\"geo\":[{1},{2}],\"typeName\":\"{3}\"}}", nameString, geoLng, geoLat, typeString);
                }
                else
                {
                    contentBuilder.AppendFormat(",{{\"name\":\"{0}\",\"geo\":[{1},{2}],\"typeName\":\"{3}\"}}", nameString, geoLng, geoLat, typeString);
                }
            }
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat("[{0}]", contentBuilder);

            string destFilePath = string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, destFileName);
            System.IO.File.WriteAllText(destFilePath, builder.ToString());
            Console.WriteLine(string.Format("转换完毕{0},count:{1}", destFilePath, dataTable.Rows.Count));
        }
    }
}